package com.tina.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tina.entity.TiLinks;
import com.tina.mapper.TiLinksMapper;
import com.tina.service.TiLinksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tina.util.TimeUtitl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author PIGS
 * @since 2020-06-05
 */
@Service
@Transactional
public class TiLinksServiceImpl extends ServiceImpl<TiLinksMapper, TiLinks> implements TiLinksService {
    @Autowired
    private TiLinksMapper tiLinksMapper;

    private Logger logger = LoggerFactory.getLogger(TiLinksServiceImpl.class);
    /**
     * 查询友情鏈接并分页
     *
     * @param tiLinks
     * @param page
     * @param limit
     * @return
     */
    @Override
    public IPage<TiLinks> queryLinkList(TiLinks tiLinks, Integer page, Integer limit) {

        AbstractWrapper wrapper = new QueryWrapper<TiLinks>();
        if (tiLinks != null) {
            if (!StringUtils.isEmpty(tiLinks.getWebsiteName())) {
                wrapper.like("website_name", tiLinks.getWebsiteName());
            }
        }
        wrapper.eq("website_state", 0);
        wrapper.orderByDesc("creation_time");
        Page<TiLinks> iPage = new Page<TiLinks>(page, limit);
        Page<TiLinks> tiLinksPage = tiLinksMapper.selectPage(iPage, wrapper);
        return tiLinksPage;
    }

    /**
     * 添加友情鏈接
     *
     * @param tiLinks
     * @return
     */
    @Override
    public Integer saveLink(TiLinks tiLinks) {
        tiLinks.setCreationTime(TimeUtitl.dateTime());
        tiLinks.setWebsiteState(0);
        Integer insert = tiLinksMapper.insert(tiLinks);
        return insert;
    }

    /**
     * 編輯友情鏈接
     *
     * @param tiLinks
     * @return
     */
    @Override
    public Integer editLink(TiLinks tiLinks) {
        Integer updateById = tiLinksMapper.updateById(tiLinks);
        return updateById;
    }

}
